﻿using LeafTransmission.Entity;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;

namespace LeafTransmission.Utils
{
    /// <summary>
    /// 网卡信息工具类
    /// author: yexiaoye
    /// </summary>
    internal class NICUtil
    {
        public static readonly string IPV4_PARATERN = @"^((([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.){3}([1-9]?\d|1\d\d|2[0-4]\d|25[0-5]))$";
        public static readonly Regex REGEX = new Regex(IPV4_PARATERN);
       
        public static List<NetworkSegmentEntity> GetAllSegment(List<string> ipv4Addresses,int selectedIndex)
        {
            List < NetworkSegmentEntity > segments = new List<NetworkSegmentEntity>();
            for (int i = 0; i < ipv4Addresses.Count; i++)
            {
                NetworkSegmentEntity segment = new NetworkSegmentEntity();
                string temp = "";
                int lastIndex = ipv4Addresses[i].LastIndexOf('.');
                temp = ipv4Addresses[i].Substring(0, lastIndex+1) + "0";
                segment.Segment = temp;
                if (i != selectedIndex)
                {
                    segment.Selected = false;
                }
                else
                {
                    segment.Selected = true;
                }

                
                segments.Add(segment);
            }
            return segments;
        }

        /// <summary>
        /// 获取所有网卡信息
        /// </summary>
        /// <returns></returns>
        public static List<NICEntity> GetAllNICSInfo()
        {
            List<NICEntity> result = new List<NICEntity>();
            
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (var item in nics)
            {
                NICEntity nicInfo = new NICEntity();
                if (item.NetworkInterfaceType == NetworkInterfaceType.Loopback) { continue; }

                nicInfo.Id = item.Id;
                nicInfo.Name = item.Name;
                nicInfo.NetworkInterfaceType = item.NetworkInterfaceType.ToString();
                nicInfo.Description = item.Description;
                nicInfo.PhysicalAddress = item.GetPhysicalAddress().ToString();
                nicInfo.Speed = item.Speed / 1000 / 1000 + "MB";
                nicInfo.OperationalStatus = item.OperationalStatus.ToString();

                IPInterfaceProperties properties = item.GetIPProperties();
                if (item.Supports(NetworkInterfaceComponent.IPv4))
                {
                    IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();                    
                    nicInfo.IPv4Mtu = ipv4.Mtu;

                    if (properties.UnicastAddresses.Count > 0)
                    {
                        // 初步ip地址判断,排除169
                        if (properties.UnicastAddresses[1].Address.ToString().Substring(0, 3) != "169")
                        {
                            nicInfo.IPv4Address = properties.UnicastAddresses[1].Address.ToString();
                            nicInfo.IPv4Mask  = properties.UnicastAddresses[1].IPv4Mask.ToString();
                            result.Add(nicInfo);
                        }

                    }
                }
                
            }
            return result;
        } 
    }
}

